package com.hainiu.cat.interview.juc;

import java.util.concurrent.CyclicBarrier;

/**
 * create by biji.zhao on 2021/7/11
 */
public class CyclicBarrierTest {


    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(4, () -> {
            // 到达栅栏后执行的回调
            System.out.println("成功到达栅栏");
        });
        for (int i = 1; i <= 4; i++) {
            final int temp = i;
            new Thread(() -> {
                System.out.println(String.format("我是线程==>%s, 收集第==>%s颗龙珠", Thread.currentThread().getName(), temp));
                try {
                    // 技术加一
                    cyclicBarrier.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, String.valueOf(i)).start();
        }
    }
}
